package com.gsm.phimhanhdong;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.google.analytics.tracking.android.EasyTracker;
import com.gsm.phimhanhdong.ContentNetworkInterface.ErrorGetPlaylistConnectionEvent;
import com.gsm.phimhanhdong.ContentNetworkInterface.GetListPlaylistSuccessEvent;
import com.gsm.phimhanhdong.ContentNetworkInterface.OnReload;
import com.readystatesoftware.viewbadger.BadgeView;

import de.greenrobot.event.EventBus;

@SuppressLint("CutPasteId")
public class MenuFragment extends android.support.v4.app.Fragment implements
		OnItemClickListener
		 {
	public static final String chanel_id = "UC-osZR0Cakxvpb2L4-8lgPA";
	ArrayList<Playlist> playlists;
	DatabaseHelper db;
	ListView listView;
	CatalougeAdapter catalougeAdapter;
	private BackgroundTasks bgTask;
	LinearLayout llLoading;
	TextView txtLoad;
	ProgressBar progressBar;
	EventBus eventBus;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		EventBus.getDefault().register(this);
		eventBus = EventBus.getDefault();
		EasyTracker.getInstance().setContext(getActivity());
		bgTask = new BackgroundTasks(getActivity());
		db = new DatabaseHelper(getActivity());
		Log.e("", "onCreate");
	}

	Button reload;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		Log.e("", "onCreateView");
		View view = inflater.inflate(R.layout.menu_layout, null);
		listView = (ListView) view.findViewById(R.id.catalouage);
		listView.setOnItemClickListener(this);
		llLoading = (LinearLayout) view.findViewById(R.id.llloading);
		txtLoad = (TextView) view.findViewById(R.id.txtLoad);
		progressBar = (ProgressBar) view.findViewById(R.id.progressbar);
		reload = (Button) view.findViewById(R.id.reload);
		reload.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				playlists = new ArrayList<Playlist>();
				eventBus.post(new OnReload());
			}
		});
		playlists = new ArrayList<Playlist>();
		bgTask.getListPlayList(chanel_id);
		return view;
	}

	public void onEventMainThread(ErrorGetPlaylistConnectionEvent e) {
		Log.e("", "ErrorGetPlaylistConnectionEvent");
		progressBar.setVisibility(View.GONE);
		txtLoad.setText("Tải dữ liệu thất bại ");
		reload.setVisibility(View.VISIBLE);
	}

	int offset = 0;
	private OnLoadListPlaylistComplete onLoadListPlaylistCompleteListener;

	public void onEventMainThread(GetListPlaylistSuccessEvent e) {
		if (e.playlists.size() > 0) {
			this.playlists = e.playlists;
			for (Playlist playlist : this.playlists) {
				if (db.checkPlaylistAvailable(playlist.id)) {
					int item_count = Integer.valueOf(db
							.getCountFromPlaylistId(playlist.id));
					if (item_count < playlist.item_count) {
						playlist.new_item_count = playlist.item_count
								- item_count;
						db.updatePlaylist(playlist.id,
								String.valueOf(playlist.item_count));
					}
				} else {
					playlist.new_item_count = playlist.item_count;
					db.insertPlayList(playlist.id,
							String.valueOf(playlist.item_count));
				}
			}
			ArrayList<Playlist> tmpPlaylists = db.getPlaylistOnlyForCheckId();
			for (Playlist playlist : tmpPlaylists) {
				boolean tmp = false;
				for (Playlist playlist1 : this.playlists) {
					if (playlist1.id.equals(playlist.id)) {
						tmp = true;
					}
				}
				if (!tmp) {
					db.deletePlaylist(playlist.id);
				}
			}
			catalougeAdapter = new CatalougeAdapter(getActivity(), playlists);
			listView.setAdapter(catalougeAdapter);
			llLoading.setVisibility(View.GONE);
		} else {
			onLoadListPlaylistFail();
		}
	}

	public class CatalougeAdapter extends BaseAdapter {
		private Context context;
		private ArrayList<Playlist> playlists;

		public CatalougeAdapter(Context context, ArrayList<Playlist> playlists) {
			super();
			this.context = context;
			this.playlists = playlists;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return playlists.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			// TODO Auto-generated method stub
			if (arg1 == null) {
				arg1 = LayoutInflater.from(context).inflate(R.layout.item_menu,
						null);
			}
			TextView txt = (TextView) arg1.findViewById(R.id.catalouage);
			txt.setText(playlists.get(arg0).title + " ( "
					+ playlists.get(arg0).getItem_count() + " clip )");
			BadgeView badgeView = new BadgeView(getActivity(), txt);
			if (playlists.get(arg0).getNew_item_count() > 0) {
				badgeView.setText("" + playlists.get(arg0).getNew_item_count());
				badgeView.show();
			} else {
				badgeView.hide();
			}
			arg1.setTag(playlists.get(arg0));
			return arg1;
		}

	}


	@Override
	public void onItemClick(AdapterView<?> arg0, View view, int pos, long arg3) {
		// TODO Auto-generated method stub
		Playlist playlist = (Playlist) view.getTag();
		String playlist_id = playlist.id;
		EasyTracker.getTracker().sendEvent(Constants.EVENT, "click item menu",
				"playlist " + playlist_id, Long.valueOf(1));
		bgTask.onMenuItemClickListenner(playlist_id, playlist.title);
	}

	public void onLoadListPlaylistFail() {
		txtLoad.setText(getResources().getString(R.string.loading_faill));
		progressBar.setVisibility(View.GONE);
		reload.setVisibility(View.VISIBLE);
		onLoadListPlaylistCompleteListener.onLoadListPlaylistFail();
	}

	public void onEventMainThread(OnReload e) {
		progressBar.setVisibility(View.VISIBLE);
		reload.setVisibility(View.GONE);
		txtLoad.setText(getResources().getString(R.string.loading));
		txtLoad.setVisibility(View.VISIBLE);
		playlists = new ArrayList<Playlist>();
		bgTask.getListPlayList(chanel_id);
	}

}
